package com.XinYun.Library.modules.Register.Dao.Impl;

import com.XinYun.Library.modules.Register.Dao.AllDao;
import com.XinYun.Library.utils.SqlUtils.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AllDaoImpl implements AllDao {
    private static Connection connection;

    /**
     * 根据用户姓名查询方向
     * @param username
     * @return
     * @throws Exception
     */
    @Override
    public String queryDirection(String username) throws Exception {
        String direction = null;
        connection = DruidUtil.getConnection();
        String sql = "select * from yunzi where username = ?;";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,username);
        ResultSet rs = ps.executeQuery();
        if (rs.next()){
            direction = rs.getString("direction");
        }
        DruidUtil.close(rs,ps,connection);
        return direction;
    }

    /**
     * 根据用户姓名查询期数
     * @param username
     * @return
     * @throws Exception
     */
    @Override
    public String queryDate(String username) throws Exception {
        String date = null;
        connection = DruidUtil.getConnection();
        String sql = "select * from yunzi where username = ?;";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,username);
        ResultSet rs = ps.executeQuery();
        if (rs.next()){
            date = rs.getString("date");
        }
        DruidUtil.close(rs,ps,connection);
        return date;
    }

    @Override
    public int queryUid(String username) throws Exception {
        int uid = 0;
        connection = DruidUtil.getConnection();
        String sql = "select uid from yunzi where username = ?;";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,username);
        ResultSet rs = ps.executeQuery();
        if (rs.next()){
            uid = rs.getInt("uid");
        }
        DruidUtil.close(rs,ps,connection);
        return uid;
    }

    @Override
    public int queryId(String sid) throws Exception {
        int id = 0;
        connection = DruidUtil.getConnection();
        String sql = "select id from register where sid = ?;";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,sid);
        ResultSet rs = ps.executeQuery();
        if (rs.next()){
            id = rs.getInt("id");
        }
        DruidUtil.close(rs,ps,connection);
        return id;
    }
}
